package com.surpassun.emall.interceptor;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.surpassun.emall.model.Province;
import com.surpassun.emall.model.User;
import com.surpassun.emall.service.PermissionService;
import com.surpassun.emall.service.ProvinceService;
import com.surpassun.emall.util.CacheUtil;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.StringPool;
import com.surpassun.emall.util.Validator;

public class ServiceActionInterceptor extends HandlerInterceptorAdapter {
	
	@Inject
	PermissionService permissionService;
	
	@Inject
	ProvinceService provinceService;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//Initialize cache
		CacheUtil.init(request);
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		if (Validator.isNotNull(modelAndView)) {
			//add the user information to attribute
			Object obj = request.getSession().getAttribute(Constants.ATTR_USER);
			if (Validator.isNotNull(obj)) {
				User user = (User)obj;
				modelAndView.addObject(Constants.ATTRIBUTE_IS_LOGIN, true);
				modelAndView.addObject(Constants.ATTRIBUTE_USER_NAME, user.getFullname());
				
				//verify if the user has admin's role
				boolean hasPermissionAccessAdmin = permissionService.hasUserPermission(user.getId(), Constants.PERMISSION_ACTION_ACCESS_ADMIN_PAGE);
				modelAndView.addObject(Constants.ATTRIBUTE_HAS_PERMISSION_ACCESS_ADMIN_PAGE, hasPermissionAccessAdmin);
			}
			
			//add the page title
			String title = request.getContextPath();
			if (Validator.isEmpty(title)) {
				title = Constants.HOME;
			}
			modelAndView.addObject(Constants.ATTRIBUTE_PAGE_TITLE, title);
			
			//add the current URL
			String originalURL = request.getServletPath();
			Map<String, String[]> parameters = request.getParameterMap();
			if (Validator.isNotEmpty(originalURL) && Validator.isNotNull(parameters) && parameters.size() > 0) {
				originalURL += StringPool.QUESTION;
				for (String key : parameters.keySet()) {
					String[] values = parameters.get(key);
					for (String value : values) {
						originalURL = originalURL + key + StringPool.EQUAL + value + StringPool.AMPERSAND; 
					}
				}
				//remove the ampersand at last
				originalURL.substring(0, originalURL.length() - 1);
			}
			modelAndView.addObject(Constants.ORIGINAL_URL, originalURL);
			
			//add the provinces for the search
			@SuppressWarnings("unchecked")
			List<Province> provinces = (List<Province>) CacheUtil.getObject(Constants.ATTR_PROVINCES);
			modelAndView.addObject(Constants.ATTR_PROVINCES, provinces);
		}
		
		super.postHandle(request, response, handler, modelAndView);
	}
}
